package com.taptech.doufu.ui.view.userdetail

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.taptech.doufu.R
import com.taptech.doufu.model.user.ListItem
import com.taptech.doufu.model.user.NovelBean
import com.taptech.doufu.model.user.NovelListBean
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity
import com.taptech.doufu.ui.adapter.UserDetailNovelListAdapter
import com.taptech.doufu.util.GsonUtil
import com.taptech.doufu.weex.http.TFHttpUtil
import com.taptech.doufu.weex.notification.QLXNotificationCenter
import kotlinx.android.synthetic.main.view_user_detail_novel_list.view.*

class UserDetailNovelListView(context: Context):LinearLayout(context) {


    var adapter:UserDetailNovelListAdapter? = null
    init {
        initView()
    }

    private fun initView() {
        val view = LayoutInflater.from(context).inflate(R.layout.view_user_detail_novel_list,null)
        addView(view)
    }

    fun setData(uid:Int,data: ListItem) {
        try {
            val info:NovelListBean = GsonUtil.parseJsonWithGson(Gson().toJson(data.data), NovelListBean::class.java)
            tvCount.text = "(${info.count}本)"
            adapter = UserDetailNovelListAdapter(context,uid,object :UserDetailNovelListAdapter.OnRefreshDataListener{
                override fun update() {
                    refreshData(info.more_url,info.list.size)
                }

            })
            adapter?.setData(info.list)
            val manager = object : LinearLayoutManager(context){
                override fun canScrollVertically(): Boolean {
                    return false
                }
            }
            mRecyclerView.layoutManager = manager
            mRecyclerView.adapter = adapter
            if (info.count <= 3) {
                //不显示右上角更多按钮
                layMore.visibility = View.GONE
            }
            layMore.setOnClickListener {
                val map = HashMap<String, Any>()
                map["more_url"] = info.more_url
                map["uid"] = uid
                SimpleWeexActivity.startActivity(context,"vue/user/TFUserNovelList.js",map)
            }
            QLXNotificationCenter.getInstance().addNotify("TFUserDetailNovelListRefresh", this) { _, _ ->
                refreshData(info.more_url,info.list.size)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private fun refreshData(url:String,size:Int) {
        val map = HashMap<String, Any>()
        map["size"] = size
        TFHttpUtil.requestGet(url,map) { json, error ->
            try {
                if (json.getInteger("status") == 0) {
                    val data = json.getJSONObject("data")
                    if (data.containsKey("list")) {
                        val array = data.getJSONArray("list")
                        val list = GsonUtil.jsonToList(array.toString(), Array<NovelBean>::class.java)
                        adapter?.setData(list)
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }
}
